package org.foundation.jala.jamerzip.modelzip;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;

import org.foundation.jala.jamerzip.components.JamerElement;
import org.foundation.jala.jamerzip.view.InformationView;
import org.foundation.jala.jamerzip.view.TableZipPanel;



public class NavigateController extends MouseAdapter{

	private String name;
	private JamerElement[] list;
	private JamerElement current;
	private ZipTableModel tableModel;
	private TableZipPanel view;
	
	public NavigateController(String name,JamerElement[] directories) {
		if(directories == null)
			throw new IllegalArgumentException("THE DIRECTORIES ARE INCORRECTS");
		this.name = name;
		this.list = directories;
		this.tableModel = new ZipTableModel();
		for (JamerElement jamerElement : directories) {
			this.tableModel.addHeader(jamerElement);
		}
		this.view = new TableZipPanel(this.tableModel);
		this.view.getInfoTable().addMouseListener(this);
		this.current = list[0];
	}
	
	public String getName() {
		return name;
	}
	
	@Override
	public boolean equals(Object obj) {
		return this.name.equals(obj);
	}
	
	public TableZipPanel getView() {
		return view;
	}
	public JamerElement[] getList() {
		return list;
	}
	public JamerElement getCurrent() {
		return current;
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		if(e.getClickCount()==2){
			int selected = getSelectedFileElement();
			InformationView info = new InformationView(new JFrame());
			info.fillData(this.list[selected]);
		}
	}
	
	public int getSelectedFileElement(){
		int selected = this.view.getInfoTable().getSelectedRow();
		return selected;
	}

	public JamerElement getFileElement(int row) {
		if(row>=this.list.length||row<0)
			row = 0;
		return this.list[row];
	}
}


